package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.Spu;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.mapper.SpuDetailMapper;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class SpuDetailServiceImpl extends ServiceImpl<SpuDetailMapper, SpuDetail> implements SpuDetailService {
    @Resource
    private SpuService spuService;
    @Resource
    private SpecParamService specParamService;

    @Override
    public List<SpecParamDTO> querySpecValues(Long id, Boolean searching) {
        Spu spu = spuService.getById(id);
        Long cid3 = spu.getCid3();
        List<SpecParamDTO> params = specParamService.queryParams(cid3, null, searching);

        SpuDetail detail = getById(id);
        String specification = detail.getSpecification();
        Map<Long, Object> map = JsonUtils.toMap(specification, Long.class, Object.class);

        for (SpecParamDTO param : params) {
            param.setValue(map.get(param.getId()));
        }

        return params;
    }
}
